package com.ruoyi.web.controller.usermanage;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.university.service.IUniversityService;
import com.ruoyi.usermanage.domain.Rider;
import com.ruoyi.usermanage.domain.User;
import com.ruoyi.usermanage.service.IRiderService;
import com.ruoyi.usermanage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生用户管理Controller
 * 
 * @author cyp
 * @date 2024-04-13
 */
@RestController
@RequestMapping("/usermanage/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;
    @Autowired
    private IUniversityService universityService;
    @Autowired
    private IRiderService riderService;

    /**
     * 查询学生用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('usermanage:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        //创建一个临时的List
        List<User> tempList = new ArrayList<>();
        //TODO 通过获取到的userlist中的序号ID，查询骑手表中的信息，使用骑手表中的认证信息，作为用户的认证状态
        for (User userInfo : list) {
            //1.获取用户ID
            Long userId = userInfo.getUserId();
            //2.根据用户ID，调用骑手信息表的查询方法，判断骑手表中是否存在指定用户
            Rider rider = riderService.selectRiderByUserId(userId);
            //  如果没有则说明为未认证状态
            if(rider != null){
                userInfo.setAuthStatus(rider.getAuthStatus());
            }
            tempList.add(userInfo);
            //  否则会有三种状态分别为：审核中，审核失败，已认证
        }

        return getDataTable(tempList);
//        return getDataTable(list);
    }

    /**
     * 导出学生用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('usermanage:user:export')")
    @Log(title = "学生用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "学生用户管理数据");
    }

    /**
     * 获取学生用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('usermanage:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(userService.selectUserByUserId(userId));
    }

    /**
     * 新增学生用户管理
     */
    @PreAuthorize("@ss.hasPermi('usermanage:user:add')")
    @Log(title = "学生用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改学生用户管理
     */
    @PreAuthorize("@ss.hasPermi('usermanage:user:edit')")
    @Log(title = "学生用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除学生用户管理
     */
    @PreAuthorize("@ss.hasPermi('usermanage:user:remove')")
    @Log(title = "学生用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(userService.deleteUserByUserIds(userIds));
    }
}
